<template>
  <div>
    <!--全局组件:分类-->
    <Category @getCategoryId="getCategoryId"></Category>
    <el-card
      shadow="never"
      body-style="{ padding: '20px' }"
      style="margin: 20px 0px"
    >
      <div v-show="scene == 0">
        <el-button
          type="primary"
          size="default"
          icon="el-icon-plus"
          :disabled="c3Id ? false : true"
          @click="addSpu"
          >添加SPU</el-button
        >
        <!-- 展示SPU(类)的table 
          el-table-column:show-overflow-tooltip 列的内容超出隐藏
       -->
        <el-table
          element-loading-text="亲稍微等等"
          v-loading="loading"
          border
          stripe
          style="margin: 20px 0px"
          :data="spuArr"
          @sort-change="handler"
        >
          <el-table-column
            label="序号"
            width="80px"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column
            prop="spuName"
            label="SPU名称"
            width
          ></el-table-column>
          <el-table-column
            prop="description"
            label="SPU描述"
            show-overflow-tooltip
            sortable
          ></el-table-column>
          <el-table-column label="操作" width>
            <template slot-scope="{ row, $index }">
              <!--组件名字：HintButton    hint-button  -->
              <hint-button
                title="添加SKU"
                icon="el-icon-plus"
                type="primary"
                size="mini"
                @click="addSku(row)"
              ></hint-button>
              <hint-button
                title="修改SPU"
                icon="el-icon-edit"
                type="warning"
                size="mini"
                @click="updateSpu(row)"
              ></hint-button>
              <hint-button
                title="查看当前SPU全部SKU"
                icon="el-icon-view"
                type="info"
                size="mini"
                @click="findSkuList(row)"
              ></hint-button>
              <hint-button
                title="删除SPU"
                icon="el-icon-delete"
                type="danger"
                size="mini"
                @click="deleteSpu(row)"
              ></hint-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器
            当前页码发生变化的事件:current-change
            当前显示数据条数发生变化:size-change
        -->
        <el-pagination
          @size-change="sizeHandler"
          @current-change="getSpuList"
          style="text-align: center"
          :current-page="page"
          :page-sizes="[4, 6, 8]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          background
          :pager-count="7"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        @changeScene="changeScene"
        ref="sku"
      ></SkuForm>
    </el-card>

    <!--对话框结合table-->

    <el-dialog title="SKU列表" :visible.sync="dialogVisibe" width="50%">
      <el-table border stripe :data="skuList" v-loading="loading1">
        <el-table-column prop="skuName" label="名称" width> </el-table-column>
        <el-table-column prop="price" label="价格" width> </el-table-column>
        <el-table-column prop="weight" label="重量" width> </el-table-column>
        <el-table-column label="默认图片" width>
          <template slot-scope="{row,$index}">
             <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px">
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer">
        <el-button @click="dialogVisibe = false">取消</el-button>
        <el-button type="primary"  @click="dialogVisibe = false">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm.vue";
import SkuForm from "./SkuForm.vue";
export default {
  components: {
    SpuForm,
    SkuForm,
  },
  name: "",
  data() {
    return {
      //分别父组件响应式数据，将来接受子组件传递过来分类的ID
      c1Id: "",
      c2Id: "",
      c3Id: "",
      //初始化参数
      page: 1, //当前页码
      limit: 4, //一页展示几条数据
      spuArr: [], //存储已有的SPU信息
      total: 0, //数据的总条数
      loading: false, //控制loading加载效果
      scene: 0,
      //存储SKU列表的数据
      skuList: [],
      //控制对话框显示与隐藏
      dialogVisibe: false,
      loading1:false,
    };
  },
  methods: {
    //排序测试回调
    handler() {
      //将来开发项目：如果有排序的业务，你可以在这里获取数据进行排序操作
    },
    //三级分类全局组件自定义事件回调
    getCategoryId({ id, flag }) {
      //父组件通过情况存储相应的分类ID
      switch (flag) {
        case "一级":
          this.c1Id = id;
          //清空二级、三级分类的ID
          this.c2Id = "";
          this.c3Id = "";
          break;
        case "二级":
          this.c2Id = id;
          //清空三级数据
          this.c3Id = "";
          break;
        case "三级":
          this.c3Id = id;
          //获取当前某一个三级分类下全部SPU
          this.getSpuList();
          break;
      }
    },
    //获取已有的分类SPU的方法
    async getSpuList(pager = 1) {
      //发请求之前：loading开始动
      this.loading = true;
      //当前分页器当前页码的时候整理参数
      this.page = pager;
      //整理参数
      const { page, limit, c3Id } = this;
      //发请求
      let result = await this.$ajax.spu.reqFindSpuInfo(page, limit, c3Id);
      if (result.code == 200) {
        //数组的替换
        this.spuArr = result.data.records;
        this.total = result.data.total;
        //服务器数据返回：loading消失
        this.loading = false;
      }
    },
    //当前table展示条数变化
    sizeHandler(limit) {
      //整理参数limit
      this.limit = limit;
      this.getSpuList();
    },
    //添加SPU按钮的回调
    addSpu() {
      this.scene = 1;
      //父组件需要通知子组件发请求【两个】
      this.$refs.spu.addSpuRequest(this.c3Id);
    },
    //修改SPU按钮的回调
    //SPU形参:当前已有的类【SPU】
    updateSpu(spu) {
      //切换场景为1
      this.scene = 1;
      //在父组件内部,获取子组件的方法,让子组件发请求，获取已有SPU相关的数据！！！
      this.$refs.spu.initSpuData(spu);
    },
    //添加SKU的回调
    addSku(spu) {
      //切换场景为2
      this.scene = 2;
      //获取子组件sku，调用子组件的方法【发请求获取数据】
      //c1Id:一级分类的ID  c3Id:二级分类的ID  spu:当前这个类的ID   三级分类的ID
      this.$refs.sku.initSkuData(this.c1Id, this.c2Id, spu);
    },
    //切换场景的回调
    changeScene({ scene, flag }) {
      //父组件切换场景即可
      this.scene = scene;
      //修改留在当前页
      if (flag == "update") {
        //获取最新的SPU数据
        this.getSpuList(this.page);
      } else if (flag == "add") {
        //添加留在第一页
        this.getSpuList(1);
      }
    },

    //查看全部实例SKU请求
    async findSkuList(row) {
      this.dialogVisibe = true;
      //发请求之前loading开始动
      this.loading1 = true;
      let result = await this.$ajax.spu.reqSkuList(row.id);
      if(result.code==200){
       this.skuList = result.data;
       //loading消失
       this.loading1 = false;
      }
    },

    //删除SPU
    async deleteSpu(row){
        try {
          await this.$ajax.spu.reqDeleteSpu(row.id); 
          //提示信息
          this.$message({type:'success',message:'删除成功'});
          //获取最新剩余SPU数据
          this.getSpuList();
        } catch (error) {
          this.$message({type:'error',message:'删除失败'});
        }
    }
  },
};
</script>

<style scoped>
</style>
